<?php
/**
 * @copyright 2011
 * @version $Id$
 * @access public
 * @author Oriotiori

 */

Class Default_Model_Login  extends Zend_Db_Table_Abstract{

	protected $_name = 'users';

	public function sendEmailNewPassword($to=array()){

		$facadeCompany= new Admin_Model_Facade_Company();
		$company=$facadeCompany->getCompanyInformation(1);
		
		ob_start();
		include_once(EMAILS_PATH.DIAG_PATH."forgotyourpassword.tpl.inc.php");
		$email_msg = ob_get_contents();
		ob_end_clean();
		
		$msg= str_replace(
						array('{IP}','{Password}', '{Owner_Name}', '{Owner_Email}', '{Url_Site}'),
						array($to['IP'], $to['Password'], $company['Owner_Name'], $company['Owner_Email'],$company['Url_Site']),
						$email_msg
		);
		

		$mail = new Zend_Mail('utf8');
		$mail->addTo($to['Email'])
				->setSubject($company['Company_Name'].' - Nueva contraseña')
				->setBodyHtml($msg)
				->setReplyTo($company['Information_Email'], $company['Information_Name'])
				->setFrom($company['Information_Email'], $company['Information_Name']);
		$sent = true;
		try {
			$mail->send();
		} catch (Exception $e){
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
			$sent = false;
		}

		return $sent;
	}

	
	public function RecoveryPassword($to=null){
		
		try{
			$data = array('Password' => md5($to['Password']));
			print_r($data);
			$where[]="Email='".$to['Email'].'\'';
			
			$res = $this->getAdapter()->update(TABLE_PREFIX.'users' ,$data, $where);
			
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage() );
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
			return false;			
		}
		
		$this->sendEmailNewPassword($to);
		return true;
	}



}


?>